
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<link rel="stylesheet" href="${path}/layui/css/layui.css" media="all">
<script src="${path}/js/jquery.min.js"></script>
<script src="${path}/layui/layui.js"></script>
<body>
<%--增加礼品--%>
    <div id="gift" style="margin-top: 20px;display:none;">
        <form class="layui-form" id="giftform" lay-filter="giftform" name="giftform">
            <input type="hidden" name="id" id="iddd">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">礼品名称：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name"  autocomplete="off"  class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">积分：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="integral"  autocomplete="off"  class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">库存：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="total"  autocomplete="off"  class="layui-input">
                    </div>
                </div>
            </div>
            <input type="hidden" name="id">
        </form>
    </div>
<%--兑换礼品--%>
<div id="duihuan" style="display: none">
    <form class="layui-form" id="duihuanform" lay-filter="duihuanform" name="duihuanform">

        <div class="layui-form-item" style="margin-top: 30px;">
            <div class="layui-inline">
                <label class="layui-form-label">预留手机号:</label>
                <div class="layui-input-inline">
                    <input type="text" id="yuliuphone"  autocomplete="off"  value="" class="layui-input" >
                </div>
            </div>
        </div>
        <div class="layui-form-item" >
            <div class="layui-inline">
                <label class="layui-form-label">积分:</label>
                <div class="layui-input-inline">
                    <input type="text"  id="jifen"  value="" class="layui-input" readonly>
                </div>
            </div>
        </div>
        <input type="hidden" name="giftname" id="lipinname">
        <input type="hidden" name="" id="vipid">
        <input type="hidden" name="integral" id="yuanjifen">
        <input type="hidden" name="namer" id="lpname">
        <input type="hidden" name="vipid" id="lpvipid">
        <input type="hidden" name="cardid" id="lpcardid">
        <input type="hidden" name="phone" id="lpphone">
        <input type="hidden" name="timer" id="lptimer">
        <input type="hidden" name="num" value="1" id="lpnum">
    </form>
</div>
<%--增加礼品记录--%>
    <form class="layui-form" action="" style="margin-top: 30px;">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">礼品名称:</label>
                <div class="layui-input-inline" style="width: 200px;">
                    <input id="name" type="text"   name="name" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline" style="width: 100px;">
                    <button type="button" class="layui-btn layui-btn-normal" onclick="SearchData();"><i class="layui-icon layui-icon-search"></i> 查询</button>
                </div>
            </div>

        </div>
    </form>
    <table class="layui-table" lay-data="{id:'diftTable',url:'${path}/gift/list', page:true,toolbar:'#toolbarDemo',defaultToolbar: ['filter', 'print', 'exports'],even: true}"
           lay-filter="diftTable">
        <thead>
        <tr>
            <th lay-data="{field:'id'}">编号</th>
            <th lay-data="{field:'name'}">礼品名称</th>
            <th lay-data="{field:'integral'}">所需积分</th>
            <th lay-data="{field:'total'}">库存</th>
            <th lay-data="{toolbar:'#barDemo'}">操作</th>
        </tr>
        </thead>
    </table>
</body>
<script>
    <%--条件查询--%>
    function SearchData() {
        layui.table.reload("diftTable",{
            page:{
                curr:1
            },
            where:{
                "name":$("#name").val()
            }
        })
    }
    /*渲染table表格*/
    layui.use(["table","form"],function() {
        var table = layui.table;
        table.on('toolbar(diftTable)',function (obj) {
            switch (obj.event) {
                case "add":
                    layer.open({
                        type: 1,
                        title: "增加礼品",
                        skin: "layui-layer-molv",
                        anim: 0,
                        offset:"120px",
                        area: "500px",
                        content: $("#gift"),
                        btn: ['确定','返回'],
                        success: function () {
                            fomrReset();
                        },
                        yes:function (index) {
                            var giftform=$("#giftform").serialize();
                            console.log(giftform);
                            $.post("/gift/add",giftform,function () {
                                layer.msg("增加成功");
                                layer.close(index);
                                table.reload('diftTable');
                            })
                        }
                    });
                    break;
            }
        })

        table.on('tool(diftTable)',function (obj) {
            var data=obj.data;
            switch (obj.event) {
                case  "update":
                    layer.open({
                        type: 1,
                        title: "修改礼品信息",
                        skin: "layui-layer-molv",
                        anim: 0,
                        offset:"120px",
                        area: "500px",
                        content: $("#gift"),
                        btn: ['确定','返回'],
                        success: function () {
                            console.log(data);
                            layui.form.val("giftform",data);
                        },
                        yes:function (index) {
                            var giftform=$("#giftform").serialize();
                            console.log(giftform);
                            $.post("/gift/update",giftform,function () {
                                layer.msg("修改成功");
                                layer.close(index);
                                table.reload('diftTable');
                            })
                        }
                    });
                    break;
                case "del":
                    layer.open({
                        time:0,
                        title:'儿童乐园友情提示！',
                        content:"确定要删除该行数据吗？",
                        shade: [0.3, '#000'],
                        btn:['确定','取消'],
                        yes : function () {
                            $.post("${path}/gift/del",{"id":data.id},function () {
                                layer.msg('删除成功');
                                table.reload('diftTable');
                            });
                        }
                    })
                    break;
                case "duihuan":
                    layer.open({
                        type: 1,
                        title: "兑换礼品",
                        skin: "layui-layer-molv",
                        anim: 0,
                        offset:"120px",
                        area: "450px",
                        content: $("#duihuan"),
                        btn: ['确定','返回'],
                        success: function () {
                            $("#yuanjifen").val(data.integral);
                            $("#lipinname").val(data.name)
                            $("#yuliuphone").val('');
                            $("#jifen").val('');
                            $("#yuliuphone").blur(function () {
                                var phone=$("#yuliuphone").val();
                                $.post("/Vip/getjifenphone",{"phone":phone},function (obj) {
                                    var yuliuphone=$("#yuliuphone").val();
                                    if(yuliuphone==null || yuliuphone==''){
                                        layer.msg("请填写手机号...");
                                        return false;
                                    }
                                    if(obj.phone==phone){
                                        $("#vipid").val(obj.id)
                                        $("#jifen").val(obj.integral);
                                        $("#lpname").val(obj.name);
                                        $("#lpvipid").val(obj.vipType.id);
                                        $("#lpcardid").val(obj.iDKaHao);
                                        $("#lpphone").val(obj.phone);
                                    }else{
                                        layer.msg("手机号不正确...",function () {});
                                    }
                                })
                                $("#jifen").val('');
                            })
                        },
                        yes:function (index) {
                            var yuliuphone=$("#yuliuphone").val();
                            if(yuliuphone==null || yuliuphone==''){
                                layer.msg("请填写手机号...");
                                return false;
                            }
                            //礼品所需的积分
                            var suoxujifen= $("#yuanjifen").val();
                            //本人已有的积分
                            var yiyoujifen= $("#jifen").val();
                            //本人id
                            var vipid= $("#vipid").val();
                            if(parseInt(suoxujifen)<=parseInt(yiyoujifen)){
                                var duihuanform=$("#duihuanform").serialize();
                                $.post("/gifttable/add",duihuanform,function () {
                                    $.post("/gifttable/updatejifen",{"jifen":suoxujifen,"id":vipid},function () {
                                        $.post("/gifttable/updatejifennum",{"id":data.id},function () {
                                            layer.msg("兑换成功...");
                                            layer.close(index);
                                            table.reload("diftTable");
                                        })
                                    })
                                })
                            }else{
                                layer.msg("抱歉您的积分不足...");
                            }

                        }
                    });
                    break;
            }
        })
    })
</script>
<%--导航按钮--%>
<script type="text/html" id="toolbarDemo">
    <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-circle"></i> 添加礼品</button>
</script>
<%--操作按钮--%>
<script type="text/html" id="barDemo">
    <a href="javascript:void (0)" lay-event="update" title="修改"><span class="layui-icon layui-icon-edit"></span></a>&ensp;
    <a href="javascript:void (0)" lay-event="del" title="删除"><span class="layui-icon layui-icon-delete"></span></a>&ensp;
    <a href="javascript:void (0)" lay-event="duihuan" title="兑换"><span class="layui-icon layui-icon-face-smile"></span></a>
</script>
<script>
    function fomrReset(){
        document.getElementById("giftform").reset();
    }
</script>
<script>
    //  获取时间
    function getNow(s) {
        return s < 10 ? '0' + s : s;
    }
    function dates() {
        var now = new Date();
        //格式化日，如果小于9，前面补0
        var day = ("0" + now.getDate()).slice(-2);
        //格式化月，如果小于9，前面补0
        var month = ("0" + (now.getMonth() + 1)).slice(-2);
        //拼装完整日期格式
        var today = now.getFullYear() + "-" + (month) + "-" + (day);
        return today;
    }
    $("#lptimer").val(dates());
</script>
</html>
